package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.skinvolume.SkinEmbroideryConfigInfo;
import com.yanqu.road.entity.config.skinvolume.SkinVolumeConfigInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/4/29 15:07
 */
public class SkinVolumeConfigDaoImpl extends TempDao {
    public static String getSkinVolumeConfigInfoSql = "select * from t_p_skin_volume";
    public Map<Integer, SkinVolumeConfigInfo> getSkinVolumeConfigInfo() throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SkinVolumeConfigInfo> configMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getSkinVolumeConfigInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                SkinVolumeConfigInfo config = new SkinVolumeConfigInfo();
                config.setVolumeId(rs.getInt("id"));
                String skinIdStr = rs.getString("skinIdList");
                String[] skinArr = skinIdStr.split("\\|");
                List<Integer> list = new ArrayList<>();
                for (String skinStr : skinArr) {
                    list.add(Integer.parseInt(skinStr));
                }
                config.setSkinIdList(list);
                config.setRewardParam(rs.getString("rewardList"));
                configMap.put(config.getVolumeId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }
    public static String getSkinEmbroideryConfigInfoSql = "select * from t_p_skin_embroidery";
    public Map<Integer, SkinEmbroideryConfigInfo> getSkinEmbroideryConfigInfo() throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SkinEmbroideryConfigInfo> configMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getSkinEmbroideryConfigInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                SkinEmbroideryConfigInfo config = new SkinEmbroideryConfigInfo();
                config.setMeter(rs.getInt("meter"));
                config.setFixNeed(rs.getInt("fixNeed"));
                config.setSkillId(rs.getInt("skillId"));
                configMap.put(config.getMeter(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }
}
